package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.ReceiveAddressVO;
import com.chinairi.mall.model.GoodsShoppingCart;
import com.chinairi.mall.service.IGoodsShoppingCartService;
import com.chinairi.mall.service.modelobject.GoodsShoppingCartMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-26
 */
@RestController
@RequestMapping("/goods-shopping-cart")
public class GoodsShoppingCartController extends BaseController {

    @Autowired
    private IGoodsShoppingCartService iGoodsShoppingCartService;

    /**
     * 添加商品购物车
     *
     * @param goodsShoppingCartList
     * @throws IOException
     */
    @PostMapping("/addGoodsToCart")
    public void addGoodsToCart(@RequestBody List<GoodsShoppingCart> goodsShoppingCartList) throws IOException {
        iGoodsShoppingCartService.addShoppingCart(goodsShoppingCartList);
        ResponseUtil.showMessage("success", "加入购物车成功", "");
    }

    /**
     * 获取某一用户的购物车
     *
     * @throws IOException
     */
    @GetMapping("/getCartGoods")
    public void getCartGoods() throws IOException {
        HashMap<String, HashMap<String, List<GoodsShoppingCartMO>>> resultMap = iGoodsShoppingCartService.getCartGoods();
        ResponseUtil.showMessage("success", "获取购物车商品成功", resultMap);
    }

    /**
     * 获取购物车中的服务
     *
     * @throws IOException
     */
    @GetMapping("/getCartService")
    public void getCartService() throws IOException {
        HashMap<String, List<Object>> resultMap = iGoodsShoppingCartService.getCartService();
        ResponseUtil.showMessage("success", "获取购物车服务成功", resultMap);
    }

    /**
     * 更新购物车商品数量
     *
     * @param goodsShoppingCart
     * @throws IOException
     */
    @PutMapping("/updateGoodsNumber")
    public void updateGoodsNumber(@RequestBody GoodsShoppingCart goodsShoppingCart) throws IOException {
        iGoodsShoppingCartService.updateGoodsNumber(goodsShoppingCart);
        ResponseUtil.showMessage("success", "商品数量修改成功", "");
    }

    /**
     * 删除购物车中的某个商品
     *
     * @param scId
     * @throws IOException
     */
    @DeleteMapping("deleteById")
    public void deleteById(@RequestParam("scId") String scId) throws IOException {
        iGoodsShoppingCartService.removeById(scId);
        ResponseUtil.showMessage("success", "商品已从购物车移除", "");
    }

    /**
     * 获取首页展示的购物车商品数量
     *
     * @throws IOException
     */
    @GetMapping("/getCartGoodsNum")
    public void getCartGoodsNum() throws IOException {
        Integer carGoodsNum = iGoodsShoppingCartService.countGoodsNum();
        ResponseUtil.showMessage("success", "获取购物车商品数量成功", carGoodsNum);
    }

    /**
     * 根据采购计划ID获取收货信息
     *
     * @throws IOException
     */
    @GetMapping("/getReceiveAddress")
    public void getReceiveAddress(@RequestParam("planPurchaseId") String planPurchaseId) throws IOException {
        ReceiveAddressVO receiveAddressVO = iGoodsShoppingCartService.getReceiveAddress(planPurchaseId);
        ResponseUtil.showMessage("success", "获取地址成功", receiveAddressVO);
    }

}
